package com.lfs;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import com.lfs.handler.ExcelHandler;
import lombok.extern.slf4j.Slf4j;

import java.awt.event.ActionEvent;

import static com.lfs.utils.GuiUtil.centerWindow;

@Slf4j
public class Gui {
    public JFrame frame;
    private JButton selectButton;

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                // 设置系统默认风格
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                log.warn("设置系统默认风格时出错", e);
            }
            Gui app = new Gui();
            app.createAndShowGUI();
        });
    }

    private void createAndShowGUI() {
        frame = new JFrame("Excel工具");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setLayout(new FlowLayout());

        selectButton = new JButton("每日数据管理表 → 问卷调查表");
        selectButton.addActionListener(this::handleSelectButton);

        frame.add(selectButton);
        centerWindow(frame);
        frame.setVisible(true);
    }

    private void handleSelectButton(ActionEvent e) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.setAcceptAllFileFilterUsed(false);
        fileChooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {
            @Override
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().toLowerCase().endsWith(".xlsx");
            }

            @Override
            public String getDescription() {
                return "Excel文件 (*.xlsx)";
            }
        });
        centerWindow(frame);
        int result = fileChooser.showOpenDialog(frame);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            new ExcelHandler().processExcelFile(selectedFile);
        }
    }








}